package client.comm.baoding.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.comm.baoding.api.bean.*
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.baoding.ui.viewmodel.MiddleVm

class MeOrderViewModel(app: Application) : MiddleVm(app) {
    val isShowing = MutableLiveData<Boolean>(false)

    val isOneLoading = MutableLiveData<Boolean>(false)
    val isZeroLoading = MutableLiveData<Boolean>(false)
    val isTwoLoading = MutableLiveData<Boolean>(false)
    val isThreeLoading = MutableLiveData<Boolean>(false)
    val isFourLoading = MutableLiveData<Boolean>(false)
    val isFiveLoading = MutableLiveData<Boolean>(false)
    val isOrderZeroLoading = MutableLiveData<Boolean>(false)

    val pageOne = MutableLiveData<Int>(0)
    val pageZero = MutableLiveData<Int>(0)
    val pageTwo = MutableLiveData<Int>(0)
    val pageThree = MutableLiveData<Int>(0)
    val pageFour = MutableLiveData<Int>(0)
    val pageFive = MutableLiveData<Int>(0)
    val pageOrderZero = MutableLiveData<Int>(0)

    val callOne = MutableLiveData<JsonResult<GoodsOrder>>()
    val callZero = MutableLiveData<JsonResult<GoodsOrder>>()
    val callTwo = MutableLiveData<JsonResult<GoodsOrder>>()
    val callThree = MutableLiveData<JsonResult<GoodsOrder>>()
    val callFour = MutableLiveData<JsonResult<GoodsOrder>>()
    val callFive = MutableLiveData<JsonResult<GoodsOrder>>()
    val cancleCall = MutableLiveData<JsonResult<Ret>>()
    val orderPayCall = MutableLiveData<JsonResult<PayUrlBean>>()
    val checkOrderPayCall = MutableLiveData<JsonResult<CheckPayBean>>()
    val rechargeCall = MutableLiveData<JsonResult<BankBind>>()

    val qrshCall = MutableLiveData<JsonResult<Ret>>()

    val callOrderZero = MutableLiveData<JsonResult<Zcjl>>()
    val callOrderOne = MutableLiveData<JsonResult<Zcjl>>()

    fun qrshCall(id: String) {
        isShowing.value = true
        hold({
            server.confirm_order(id)
        }, success = {
            qrshCall.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun orderPay(id: String) {
        isShowing.value = true
        hold({
            server.order_pay(id)
        }, success = {
            orderPayCall.value = it
        }, complete = {
            isShowing.value = false
        })
    }


    fun rechargeOrderPay(id: String) {
        isShowing.value = true
        hold({
            server.trading_order_pay(id)
        }, success = {
            rechargeCall.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun cancle(id: String) {
        isShowing.value = true

        hold({
            server.del_buy_order(id)
        }, success = {
            cancleCall.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun check_order_pay(trade_no: String) {
        isShowing.value = true

        hold({
            server.check_order_pay(trade_no)
        }, success = {
            checkOrderPayCall.value = it
        }, complete = {
            isShowing.value = false
        })
    }


    fun holdOne() {
        if (isOneLoading.value!!)
            return
        hold({
            server.buy_order_record(pageOne.value!!, -1)
        }, {
            isOneLoading.value = true
            pageOne.value = pageOne.value!! + 1
        }, {
            callOne.value = it
        }, complete = {
            isOneLoading.value = false
        })
    }

    fun holdZero() {
        if (isZeroLoading.value!!)
            return
        hold({
            server.buy_order_record(pageZero.value!!, 0)
        }, {
            isZeroLoading.value = true
            pageZero.value = pageZero.value!! + 1
        }, {
            callZero.value = it
        }, complete = {
            isZeroLoading.value = false
        })
    }

    fun holdTwo() {
        if (isTwoLoading.value!!)
            return
        hold({
            server.buy_order_record(pageTwo.value!!, 1)
        }, {
            isTwoLoading.value = true
            pageTwo.value = pageTwo.value!! + 1
        }, {
            callTwo.value = it
        }, complete = {
            isTwoLoading.value = false
        })
    }

    fun holdThree() {
        if (isThreeLoading.value!!)
            return
        hold({
            server.buy_order_record(pageThree.value!!, 2)
        }, {
            isThreeLoading.value = true
            pageThree.value = pageThree.value!! + 1
        }, {
            callThree.value = it
        }, complete = {
            isThreeLoading.value = false
        })
    }

    fun holdFour() {
        if (isFourLoading.value!!)
            return
        hold({
            server.buy_order_record(pageFour.value!!, 3)
        }, {
            isFourLoading.value = true
            pageFour.value = pageFour.value!! + 1
        }, {
            callFour.value = it
        }, complete = {
            isFourLoading.value = false
        })
    }

    fun holdFive() {
        if (isFiveLoading.value!!)
            return
        hold({
            server.buy_order_record(pageFive.value!!, 4)
        }, {
            isFiveLoading.value = true
            pageFive.value = pageFive.value!! + 1
        }, {
            callFive.value = it
        }, complete = {
            isFiveLoading.value = false
        })
    }


    fun holdOrderZero() {//order_id: String
        if (isOrderZeroLoading.value!!)
            return
        hold({
            server.recharge_record(pageOrderZero.value!!, "0")
        }, {
            isOrderZeroLoading.value = true
            pageOrderZero.value = pageOrderZero.value!! + 1
        }, {
            callOrderZero.value = it
        }, complete = {
            isOrderZeroLoading.value = false
        })
    }

    fun holdOrderOne() {//order_id: String
        if (isOneLoading.value!!)
            return
        hold({
            server.recharge_record(pageOne.value!!, "1")
        }, {
            isOneLoading.value = true
            pageOne.value = pageOne.value!! + 1

        }, {
            callOrderOne.value = it
        }, complete = {
            isOneLoading.value = false
        })
    }


    fun trading_cancle(id: String) {
        isShowing.value = true

        hold({
            server.trading_del_order(id)
        }, success = {
            cancleCall.value = it
        }, complete = {
            isShowing.value = false
        })
    }


}